$(document).ready(function()
    {
        var ju, js, dp, dk, cm, ds, dpg, dpd, total, shu, old_year;

         $('.date-picker').datepicker( {
            changeYear: true,
            showWeek: true,
            showButtonPanel: true,
            dateFormat: 'yy',
            onClose: function(dateText, inst) {
                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                $(this).datepicker('setDate', new Date(year, 1, 1));
            }
        });


        $.calc_persen = function()
        {
            shu = parseFloat($("#JUMLAH_SHU").val());
            ju = parseFloat($("#JASA_USAHA").val());
            $("#R_JASA_USAHA").html(ju * shu / 100);
            js = parseFloat($("#JASA_SIMPANAN").val());
            $("#R_JASA_SIMPANAN").html(js * shu / 100);
            dp = parseFloat($("#DANA_PENGURUS").val());
            $("#R_DANA_PENGURUS").html(dp * shu / 100);
            dk = parseFloat($("#DANA_KESEJAHTERAAN").val());
            $("#R_DANA_KESEJAHTERAAN").html(dk * shu / 100);
            cm = parseFloat($("#CADANGAN_MODAL").val());
            $("#R_CADANGAN_MODAL").html(cm * shu / 100);
            ds = parseFloat($("#DANA_SOSIAL").val());
            $("#R_DANA_SOSIAL").html(ds * shu / 100);
            dpg = parseFloat($("#DANA_PENGEMBANGAN").val());
            $("#R_DANA_PENGEMBANGAN").html(dpg * shu / 100);
            dpd = parseFloat($("#DANA_PENDIDIKAN").val());
            $("#R_DANA_PENDIDIKAN").html(dpd * shu / 100);
            total = ju + js + dp + dk + cm + ds + dpg + dpd;
            $("#TOTAL").html(total + "");
            $("#R_TOTAL").html(total * shu / 100);
        }

        $.notif_distribusi = function(data)
        {
            if(data.status == "EXIST")
            {
                if(confirm("Data Distribusi untuk tahun ini sudah ada, Apakah data akan diganti?"))
                    $.data_ajax('br-05/distribusi/update', "#FORM_DISTRIBUSI", $.notif_distribusi);
            }
            else
                alert("Data sudah disimpan");
        }

        $.simpan_distribusi = function()
        {
            if(confirm("Apakah data sudah benar dan akan menyimpan Distribusi SHU untuk tahun ini?"))
                $.data_ajax('br-05/distribusi/shu', "#FORM_DISTRIBUSI", $.notif_distribusi);
        }
        
        $.notif_get = function(data)
        {
            if(data.status == "EXIST")
                {
                    $.set_data("#FORM_DISTRIBUSI", data.data[0]);
                    $.calc_persen();
                }
        }

        $.get_distibusi = function()
        {
            if($("#BULAN_DISTRIBUSI").val() != "" && old_year != $("#BULAN_DISTRIBUSI").val())
                $.data_ajax('br-05/distribusi/prepare_shu/' + $("#BULAN_DISTRIBUSI").val(), "#FORM_DISTRIBUSI", $.notif_get);
            old_year = $("#BULAN_DISTRIBUSI").val();
        }

        $("#JASA_USAHA").change($.calc_persen);
        $("#JASA_SIMPANAN").change($.calc_persen);
        $("#DANA_PENGURUS").change($.calc_persen);
        $("#DANA_KESEJAHTERAAN").change($.calc_persen);
        $("#CADANGAN_MODAL").change($.calc_persen);
        $("#DANA_SOSIAL").change($.calc_persen);
        $("#DANA_PENGEMBANGAN").change($.calc_persen);
        $("#DANA_PENDIDIKAN").change($.calc_persen);
        $("#FORM_DISTRIBUSI").submit(function(){return false;});
        $("#ADD").click($.simpan_distribusi);
        $("#BULAN_DISTRIBUSI").blur($.get_distibusi);

        $.calc_persen();
    });